import es
import random
import playerlib
 
import wcs

raceName = "Human Alliance"
race     = wcs.races[raceName]

""" Load the strings into the variable text """
text     = race.loadStrings()

""" ############################### """
""" ### Begin the configuration ### """
""" ############################### """

config   = race.config()

config.text("")
config.text("********************")
config.text("Invisibility")
config.text("  This skill allows a playe to spawn with a higher invisibility effect")
config.text("********************")
config.text("")
invisMaxlevel      = config.cvar("invisMaxlevel",      6,  "The maximum level of the Invisibility skill")
invisLevelInterval = config.cvar("invisLevelInterval", 2,  "The intervals between the abilities to upgrade their Invisibility skill")
invisibilityRating = config.cvar("invisibilityRating", 20, "The effect that the invisibility skill will have... A relativly high value is 30, whereas a low value is 10.")

config.text("")
config.text("********************")
config.text("Devotion")
config.text("  This skill allows a player to spawn with more health")
config.text("********************")
config.text("")
devotMaxlevel        = config.cvar("devotMaxlevel",         4, "The maximum level of the Devotion skill")
devotLevelInterval   = config.cvar("devotLevelInterval",    1, "The intervals between the abilities to upgrade their Devotion skill")
devotHealthIncrement = config.cvar("devotHealthIncrement", 15, "The amount of health devotion gives the player, multiplied by their level") 

config.text("")
config.text("********************")
config.text("Bash")
config.text("  This skill has a chance to stun enemies")
config.text("  every time the player attacks another")
config.text("********************")
config.text("")
bashMaxlevel      = config.cvar("bashMaxlevel",      4, "The maximum level of the Bash skill")
bashLevelInterval = config.cvar("bashLevelInterval", 3, "The intervals between the abilities to upgrade their bash skill")
bashPercentage    = config.cvar("bashPercentage",    8, "The chance a player will stun another when they damage, multiplied via their level")

config.text("")
config.text("********************")
config.text("Avatar")
config.text("  This ultimate gives the player")
config.text("  better attributes for a certain")
config.text("  time, including health, speed and")
config.text("  extra damage. However, when the effect")
config.text("  runs out, the player will be severely")
config.text("  vulnerable.")
config.text("********************")
config.text("")
avararMinLevel      = config.cvar("avararMinLevel",      8, "The level required before a player can buy level 1 in the Avatar Skill")
avatarMaxLevel      = config.cvar("avatarMaxLevel",      3, "The maximum level of the Avatar skill")
avatarLevelInterval = config.cvar("avatarLevelInterval", 4, "The intervals between the abilities to upgrade their Avatar skill")
avatarDuration      = config.cvar("avatarDuration",      4, "The number of seconds multiplied via their level that the Avatar effect will last for")
avatarCoolDown      = config.cvar("avatarCoolDown",      8, "The amount of seconds after the avatar effect has worn off before the player can use the skill again.")

config.load(True)

""" ##################### """
""" ### END OF CONFIG ### """
""" ##################### """

race.registerSkill("Invisibility", int(invisMaxlevel), int(invisLevelInterval), "Makes you partially invisible.")
race.registerSkill("Devotion", int(devotMaxlevel), int(devotLevelInterval), "Gives you additional health each round.")
race.registerSkill("Bash", int(bashMaxlevel), int(bashLevelInterval), "Have a chance to stun the person you hit with an attack.")
race.registerUltimate("Avatar", int(avararMinLevel), int(avatarLevelInterval), int(avatarLevelInterval), "Hold down your ultimate key to get stronger for a short time.")

def player_spawn(event_var):
    """
    Executed when a player spawns, ensure that we reassign the player"s alpha
    to make them more transparent if they have the required level to make them
    more invisible.
    """
    userid = int(event_var["userid"])
    player = wcs.players[userid]
    setAlpha(userid)
    devotion = player[raceName]["Devotion"]
    if devotion:
        devotion *= int(devotHealthIncrement)
        player.healthAdd(devotion)
        es.tell(userid, "#multi", text("devotion", {"health" : devotion}, playerlib.getPlayer(userid).get("lang")))
    
def item_pickup(event_var):
    """
    Executed when an item is picked up. Reapply the alpha colour scale so that
    the players weapon colour is reassigned.
    """
    userid = int(event_var["userid"])
    setAlpha(userid)
        
def player_hurt(event_var):
    """
    Executed when a player is hurt, ensure we handle the bash proc and the
    avatar additional damage if they are currently in their ultimate state.
    
    First check if the player has the bash level, if so, then freeze the player
    for 4 seconds depending on the dice roll and whether or not the proc
    ocurred.
    
    Second check if they are in the avatar state, if so, then apply additional
    damage.
    """
    userid = int(event_var["userid"])
    attacker = int(event_var["attacker"])
    wcsAttacker = wcs.players[attacker]
    wcsUserid = wcs.players[userid]
    if attacker:
        damage = wcs.getDamage(event_var)
        bash   = wcsAttacker[raceName]["Bash"] * int(bashPercentage)
        if bash >= random.randint(1, 100):
            wcsUserid.freeze(4)
            es.tell(attacker, "#multi", text("bash", {"victim" : es.getplayername(userid)}, playerlib.getPlayer(attacker).get("lang")))
        avatar = wcsAttacker[raceName]["Avatar"]
        if avatar:
            state = wcsAttacker["avatar"]
            if state is True:
                wcsUserid.damage(int(damage * .3), 32, attacker)
                    
def player_ultimate_on(event_var):
    """
    Executed when a player activates their ultimate. If they currently have a
    level in their ultimate (Avatar), then ensure that we buff their health and
    assign their "avatar" state as True so additional damage can be applied when
    they hurt other victims.
    """
    userid = int(event_var["userid"])
    player = wcs.players[userid]
    avatar = player[raceName]["Avatar"] * int(avatarDuration)
    if avatar and not player["avatar"]:
        es.tell(userid, "#multi", text("avatar on", {"time" : avatar}, playerlib.getPlayer(userid).get("lang")))
        player["avatar"] = True
        player.healthAdd(100)
        player.color(blue=255)
        player.delayed(avatar, disableAvatar, userid)
        
def setAlpha(userid):
    """
    Assigns the alpha value of a player. This should be called every time we
    want to apply the colour to a user if they have a specific level within the
    invisibility value.
    
    @param int userid The ID of the user we wish to reassign the alpha
    """
    player = wcs.players[userid]
    invisibility = player[raceName]["Invisibility"]
    if invisibility:
        newAlpha = 255 - (invisibility * int(invisibilityRating) + 90)
        player.color(alpha=newAlpha)
        
def disableAvatar(userid):
    """
    Executed when the players ultimate has come off of the time limit. Ensure
    that their avatar status is removed, and their buffs removed.
    
    @param int userid The ID of the user we wish to remove the avatar status from
    """
    player = wcs.players[userid]
    state = player["avatar"]
    if state is True:
        es.tell(userid, "#multi", text("avatar off", lang=playerlib.getPlayer(userid).get("lang"))) 
        player.setCoolDown("Avatar", int(avatarCoolDown))
        setAlpha(userid)
        if es.getplayerprop(userid, "CBasePlayer.m_iHealth") >= 101:
            player.healthAdd(-100)
        else:
            player.health(1)
        del player["avatar"] # Remove the state check
